<?php
class item_musicplayer extends template_item
{
	var $default_name = "Add a Song";
	var $default_description = "Add a Song to your profile";
	
	var $allow_multiple = 1;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['song'] = "";
			$this->item_config['upload'] = 1;
			$this->item_config['max_size'] = 5120;
		}
	}
	
	function admin_form()
	{
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>What song to add?</b><br />Only MP3 are accepted. Leave this blank for the user to pick his/her own song. Put {remove} to just remove the song.",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][song]", $this->item_config['song']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>If the user pick his/her song, allow them to upload their own MP3?</b>",
															$this->ipsclass->adskin->form_yes_no("item[".$this->item['item_id']."][upload]", $this->item_config['upload']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>If you allow to upload a song, who big is the song?</b><br />Input 0 for no limit. In KiB.",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][max_size]", $this->item_config['max_size']),
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$upload_path  = $this->ipsclass->vars['upload_dir'];
		if ( ! is_dir( $upload_path . "/profile_song" ) )
		{
			if ( @mkdir( $upload_path . "/profile_song", 0777 ) )
			{
				@chmod( $upload_path . "/profile_song", 0777 );
				
				# Set path and dir correct
				$upload_path .= "/profile_song";
				$upload_dir   = "profile_song/";
			}
			else
			{
				$upload_dir   = "";
			}
		}
		else
		{
			# Set path and dir correct
			$upload_path .= "/profile_song";
			$upload_dir   = "profile_song/";
		}
		
		$save = array();
		$save['song'] = $input['song'];
		$save['upload'] = $input['upload'];
		$save['max_size'] = intval($input['max_size']);
		$save['song_loc'] = $upload_path;
		$save['song_dir'] = $upload_dir;
		
		$item_info['item_config'] = serialize($save);
		
		if($this->ipsclass->DB->field_exists("pp_song", "profile_portal"))
		{
			$this->ipsclass->DB->sql_change_field("profile_portal","pp_song","pp_song","varchar(255) NOT NULL","''");
		}
		else
		{
			$this->ipsclass->DB->sql_add_field("profile_portal","pp_song","varchar(255) NOT NULL","''");
		}
	}
	
	function admin_view_file_edits()
	{
		$edits = "";
		
		$edits .= $this->ipsclass->adskin->start_table("Files to edit");
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"/sources/action_public/profile.php",
														)	);
		$edits .= $this->ipsclass->adskin->end_table();

		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		
		$edits .= $this->ipsclass->adskin->start_table("/sources/action_public/profile.php");
		$edits .= $this->ipsclass->adskin->add_td_basic('About line 3898', 'left', 'tablesubheader');
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Find",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>    	\$info['members_display_name'] = \$member['members_display_name'];\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Under the function",
																"view_profile()",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Add after",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>    	\$info['pp_song'] = \$member['pp_song'];\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"It will look like",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>    	\$info['members_display_name'] = \$member['members_display_name'];\n    	\$info['pp_song'] = \$member['pp_song'];\n</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->end_table();
		
		$edits .= $this->ipsclass->adskin->start_table("Templates to edit");
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																			"Profile View Skin<br />- personal_portal_main<br />- show_profile<br />Topic View Skin<br />- RenderRow",
																	)	);
		$edits .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		
		$edits .= $this->ipsclass->adskin->start_table("Topic View Skin");
		$edits .= $this->ipsclass->adskin->add_td_basic('RenderRow', 'left', 'tablesubheader');
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"How to get there",
																"Go click the LOOK & FEEL tab on top or <a href='".$this->ipsclass->base_url."&amp;section=lookandfeel' target='_blank'>here</a> open a new window.<br />Click Skin Manager on the left.<br />On each skin, clikc the options icon and click Edit Template HTML.<br />Click Topic View, then click RenderRow on the right side.",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Find",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>{\$author['member_rank_img']}</textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Add after",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'><if=\"\$author['pp_song']\"><br /><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"91\" height=\"15\"><param name=\"movie\" value=\"{\$this->ipsclass->vars['board_url']}/images/market/xspf_player_slim.swf?song_url={\$author['pp_song']}&autoplay=false\" /><param name=\"allowScriptAccess\" value=\"sameDomain\"/><param name=\"quality\" value=\"high\"/><param name=\"bgcolor\" value=\"#E6E6E6\"/><embed src=\"{\$this->ipsclass->vars['board_url']}/images/market/xspf_player_slim.swf?song_url={\$author['pp_song']}&autoplay=false\"  quality=\"high\" bgcolor=\"#E6E6E6\" name=\"xspf_player\" allowscriptaccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" align=\"center\" height=\"15\" width=\"91\"></object></if></textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"It should look like",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'>{\$author['member_rank_img']}<if=\"\$author['pp_song']\"><br /><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"91\" height=\"15\"><param name=\"movie\" value=\"{\$this->ipsclass->vars['board_url']}/images/market/xspf_player_slim.swf?song_url={\$author['pp_song']}&autoplay=false\" /><param name=\"allowScriptAccess\" value=\"sameDomain\"/><param name=\"quality\" value=\"high\"/><param name=\"bgcolor\" value=\"#E6E6E6\"/><embed src=\"{\$this->ipsclass->vars['board_url']}/images/market/xspf_player_slim.swf?song_url={\$author['pp_song']}&autoplay=false\"  quality=\"high\" bgcolor=\"#E6E6E6\" name=\"xspf_player\" allowscriptaccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" align=\"center\" height=\"15\" width=\"91\"></object></if></textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->end_table();
		
		$edits .= $this->ipsclass->adskin->start_table("Profile View Skin");
		$edits .= $this->ipsclass->adskin->add_td_basic('personal_portal_main', 'left', 'tablesubheader');
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"How to get there",
																"Click Profile View on the bottom left, then click personal_portal_main on the bottom right side.",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Find on the top",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'><p>{\$member['_member_rank_img']}</p></textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Add after",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'><if=\"\$member['pp_song']\"><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"91\" height=\"15\"><param name=\"movie\" value=\"{\$this->ipsclass->vars['board_url']}/images/market/xspf_player_slim.swf?song_url={\$member['pp_song']}&autoplay=true\" /><param name=\"allowScriptAccess\" value=\"sameDomain\"/><param name=\"quality\" value=\"high\"/><param name=\"bgcolor\" value=\"#E6E6E6\"/><embed src=\"{\$this->ipsclass->vars['board_url']}/images/market/xspf_player_slim.swf?song_url={\$member['pp_song']}&autoplay=true\"  quality=\"high\" bgcolor=\"#E6E6E6\" name=\"xspf_player\" allowscriptaccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" align=\"center\" height=\"15\" width=\"91\"></object></if></textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"It should look like",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'><p>{\$member['_member_rank_img']}</p><if=\"\$member['pp_song']\"><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"91\" height=\"15\"><param name=\"movie\" value=\"{\$this->ipsclass->vars['board_url']}/images/market/xspf_player_slim.swf?song_url={\$member['pp_song']}&autoplay=true\" /><param name=\"allowScriptAccess\" value=\"sameDomain\"/><param name=\"quality\" value=\"high\"/><param name=\"bgcolor\" value=\"#E6E6E6\"/><embed src=\"{\$this->ipsclass->vars['board_url']}/images/market/xspf_player_slim.swf?song_url={\$member['pp_song']}&autoplay=true\"  quality=\"high\" bgcolor=\"#E6E6E6\" name=\"xspf_player\" allowscriptaccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" align=\"center\" height=\"15\" width=\"91\"></object></if></textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_basic('show_profile', 'left', 'tablesubheader');
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"How to get there",
																"Click show_profile on the bottom right side.",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Find on the top",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'><div>{\$info['member_rank_img']}</div></textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"Add after",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'><if=\"\$info['pp_song']\"><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"91\" height=\"15\"><param name=\"movie\" value=\"{\$this->ipsclass->vars['board_url']}/images/market/xspf_player_slim.swf?song_url={\$info['pp_song']}&autoplay=true\" /><param name=\"allowScriptAccess\" value=\"sameDomain\"/><param name=\"quality\" value=\"high\"/><param name=\"bgcolor\" value=\"#E6E6E6\"/><embed src=\"{\$this->ipsclass->vars['board_url']}/images/market/xspf_player_slim.swf?song_url={\$info['pp_song']}&autoplay=true\"  quality=\"high\" bgcolor=\"#E6E6E6\" name=\"xspf_player\" allowscriptaccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" align=\"center\" height=\"15\" width=\"91\"></object></if></textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->add_td_row(array(
																"It should look like",
																"<textarea rows='5' cols='50' class='multitext' style='width:98%'><div>{\$info['member_rank_img']}</div><if=\"\$info['pp_song']\"><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"91\" height=\"15\"><param name=\"movie\" value=\"{\$this->ipsclass->vars['board_url']}/images/market/xspf_player_slim.swf?song_url={\$info['pp_song']}&autoplay=true\" /><param name=\"allowScriptAccess\" value=\"sameDomain\"/><param name=\"quality\" value=\"high\"/><param name=\"bgcolor\" value=\"#E6E6E6\"/><embed src=\"{\$this->ipsclass->vars['board_url']}/images/market/xspf_player_slim.swf?song_url={\$info['pp_song']}&autoplay=true\"  quality=\"high\" bgcolor=\"#E6E6E6\" name=\"xspf_player\" allowscriptaccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" align=\"center\" height=\"15\" width=\"91\"></object></if></textarea>",
														)	);
		$edits .= $this->ipsclass->adskin->end_table();

		return $edits;
	}
	
	function admin_remove()
	{
		if(count($this->ipsclass->market_class->item_by_folder[$this->item['filename']]) == 1)
		{
			$this->ipsclass->DB->sql_drop_field("profile_portal","pp_song");
		}
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
		$upload = "";
		if($this->item_config['upload'])
		{
			$upload = <<<EOF
		<tr>
			<td class='darkrow3' align='center' colspan='2'>
				<b>{$this->ipsclass->lang['_or_']}</b>
			</td>
		</tr>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['upload_a_song']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='file' size='30' name='upload_song' />
			</td>
		</tr>
EOF;
		}
		
		return <<<EOF
<div class='formsubtitle'>{$this->ipsclass->lang['profile_song']}</div>
<form action='{$this->base_url}' method='post' enctype='multipart/form-data'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['pick_song_url']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='input' name='song_url' value='' size='30' class='forminput' />
			</td>
		</tr>
{$upload}
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['add_song']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
EOF;
	}
	
	function input_done()
	{
		return $this->item_config['song'] != "" || isset($this->ipsclass->input['item_used']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$song = "";
		
		// Remove Song
		if(file_exists($this->item_config['song_loc']."/song-".$this->ipsclass->member['id'].".mp3"))
		{
			@unlink($this->item_config['song_loc']."/song-".$this->ipsclass->member['id'].".mp3");
		}
		if($this->ipsclass->vars['upload_dir'] != $this->item_config['song_loc'] && file_exists($this->ipsclass->vars['upload_dir']."/song-".$this->ipsclass->member['id'].".mp3"))
		{
			@unlink($this->ipsclass->vars['upload_dir']."/song-".$this->ipsclass->member['id'].".mp3");
		}
		
		if($this->item_config['song'] != "" || $this->item_config['song'] == "{remove}")
		{
			$song = $this->item_config['song'] == "{remove}" ? "" : $this->item_config['song'];
		}
		elseif($this->item_config['upload'] && isset($_FILES['upload_song']) && is_uploaded_file($_FILES['upload_song']['tmp_name']))
		{
			require_once(KERNEL_PATH."class_upload.php");
			$upload = new class_upload();
			$upload->ipsclass = &$this->ipsclass;
			
			$upload->out_file_name = 'song-'.$this->ipsclass->member['id'];
			$upload->out_file_dir = $this->item_config['song_loc'];
			$upload->max_file_size = $this->item_config['max_size'] * 1024;
			$upload->allowed_file_ext = array('mp3');
			$upload->upload_form_field = 'upload_song';
			
			$upload->upload_process();
			
			if($upload->error_no)
			{
				$this->ipsclass->DB->do_update( 'profile_portal', array(
													  				   	 'pp_song'      => "",
																	 ),  'pp_member_id  ='.$this->ipsclass->member['id'] );
				
				switch( $upload->error_no )
				{
					case 1:
						$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'upload_failed' ) );
					case 2:
						$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'no_av_type' ) );
					case 3:
						$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'upload_to_big') );
					case 4:
						$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'upload_failed' ) );
					case 5:
						$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'upload_failed' ) );
				}
			}
			
			$song = $this->ipsclass->vars['upload_url']."/".$this->item_config['song_dir'].$upload->parsed_file_name;
			
		}
		else
		{
			$song = $this->ipsclass->input['song_url'];
		}
		
		$this->ipsclass->DB->simple_construct( array( 'select' => 'pp_member_id',
													  'from'   => 'profile_portal',
													  'where'  => "pp_member_id=".$this->ipsclass->member['id'] ) );
		$this->ipsclass->DB->simple_exec();
	
		if ( $this->ipsclass->DB->get_num_rows() )
		{
			# Update...
			$this->ipsclass->DB->do_update( 'profile_portal', array(
												  				   	 'pp_song'      => $song,
																 ),  'pp_member_id  ='.$this->ipsclass->member['id'] );
		}
		else
		{
			# Insert
			$this->ipsclass->DB->do_insert( 'profile_portal', array(
												  				   	 'pp_song'      => $song,
																	 'pp_member_id'	=> $this->ipsclass->member['id'],
																 ) );
		}
		
		$this->delete_items($inventory_ids);
		
		$inv_id = reset($inventory_ids);
		$this->_write_log($inv_id, "Added song to profile");
		
		$this->ipsclass->market_class->redirect('song_profile_made');
	}
}
?>